<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
if ( !ican('tag') ) msg('对不起，你没有权限执行本操作！');

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';

$message = array(
	1 => '标签已更新。',
	2 => '标签已删除。',
	3 => '标签添加成功。',
	4 => '标签修改成功。',
	5 => '标签合并完成，请刷新缓存。'
);

if ($f == 'index') {
	$page=getGP('page');
	$min=getGP('min');
	$max=getGP('max');
	$keyword=getGP('keyword');
	$page = max(1, intval($page));
	$pagesize = 20;
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?m=tag';
	$wheresql = '';
	if ( $keyword ) {
		$wheresql .= " AND tagname LIKE '%$keyword%'";
		$url .= '&keyword='.rawurlencode($keyword);
	}
	if ( $min !== '' ) {
		$wheresql .= " AND `total` >= ".intval($min);
		$url .= "&min=$min";
	}
	if ( $max !== '' ) {
		$wheresql .= " AND `total` <= ".intval($max);
		$url .= "&max=$max";
	}
	$result = array();
	$num = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."tags WHERE 1 $wheresql");
	if ( $num ) {
		$result = $_G['db']->fetch_all("SELECT * FROM ".DB_PREFIX."tags WHERE 1 $wheresql ORDER BY `total` DESC LIMIT $offset, $pagesize");
	}
	include admintemplate('tag');

} elseif ($f == 'add' || $f == 'edit') {

	if ( check_submit('dosubmit') ) {
		$savetype=getGP('savetype');
		$id=getGP('id');
		$tagname=getGP('tagname');
		if ( empty($tagname) ) msg('标签名称不能为空。');
		if ( $savetype == 'add' ) {
			if ( $_G['db']->fetch_one_array("SELECT tagid FROM ".DB_PREFIX."tags WHERE tagname = '".check_str($tagname)."'") ) {
				 msg("标签 <b>$tagname</b> 已存在。");
			}
			$_G['db']->insert('tags',array('tagname'=>check_str($tagname)));
		} else {
			$_G['db']->query("UPDATE ".DB_PREFIX."tags SET tagname = '".check_str($tagname)."' WHERE tagid = '".intval($id)."'");
		}
		recache('tags');
		msg($message[$savetype=='add'?3:4],'admin.php?m=tag');
	} else {
		$result = array();
		if ( $f == 'edit') {
			$id = getGP('id','G','int');
			$result = $_G['db']->fetch_one_array("SELECT * FROM ".DB_PREFIX."tags WHERE tagid = '$id'");
			if (!$result) {
				msg('标签不存在！');
			}
		}else{
			$result= array('tagname'=>null,'tagid'=>null);
		}
		include admintemplate('tag');
	}

} else {

	if ( check_submit('dosubmit') ) {

		$idarr = getGP('id','P','array');
		if ( empty($idarr) ) msg('请选择要执行操作的标签。');

		if ( $f == 'update' ) {

			foreach ($idarr as $id) {
				recount_tag($id);
			}
			recache('tag');
			msg($message[1]);

		}  elseif ( $f == 'delete' ) {

			foreach ($idarr as $id) {
				delete_tag($id);
			}
			recache('tag');
			msg($message[2]);

		} elseif ( $f == 'merge' ) {
		
			$newtag = getGP('newtag','P');
			if ( strlen($newtag) < 1 || strlen($newtag) > 20 ) {
				msg('合并目的标签长度应在1-20字节之间。');
			}
			$newtag = htmlspecialchars(trim($newtag));
			$tagid = $_G['db']->result("SELECT tagid FROM ".DB_PREFIX."tags WHERE tagname = '$newtag'");
			if ( !$tagid ) {
				$tagid = $_G['db']->insert('tags', array('tagname'=>$newtag));
			}
			foreach ( $idarr as $id ) {
				$id = intval($id);
				if ( $id == $tagid ) continue;
				$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = '$tagid' WHERE tags = '$id'");
				$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = REPLACE(tags, ',$id',',$tagid') WHERE tags <> ''");
				$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = REPLACE(tags, '$id,','$tagid,') WHERE tags <> ''");
			}
			$_G['db']->query("DELETE FROM ".DB_PREFIX."tags WHERE tagid IN (".implode(',', $idarr).") AND tagid != $tagid");
			msg($message[5]);

		}
	}

}

function delete_tag($id) {
	global $_G;
	$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = '' WHERE tags = '$id'");
	$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = REPLACE(tags, ',$id','') WHERE tags <> ''");
	$_G['db']->query("UPDATE ".DB_PREFIX."article SET tags = REPLACE(tags, '$id,','') WHERE tags <> ''");
	$_G['db']->query("DELETE FROM ".DB_PREFIX."tags WHERE tagid = '$id'");
}

function recount_tag($tagid) {
	global $_G;
	$num = 0;
	$num = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."article WHERE (tags = '$tagid') OR (tags LIKE '$tagid,%') OR (tags LIKE '%,$tagid') OR (tags LIKE '%,$tagid,%')");
	$_G['db']->query("UPDATE ".DB_PREFIX."tags SET `total` = '$num' WHERE tagid = '$tagid'");
}
?>